﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Web.Mvc;
using System.Web.Routing;
using Enter.Mvc.Base.Infrastructure.ActionFilters;
using Enter.Mvc.Base.Infrastructure;
using Enter.Mvc.Base.Model;
using Enter.Mvc.Base.Service;

namespace Enter.Mvc.Base
{
    public class BaseApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public virtual void RegisterRoutes(RouteCollection routes)
        {
            // routes.Clear();
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //添加默认路由
            General(routes);

        }



        protected void Application_Start()
        {

            OnStart();


            //AreaRegistration.RegisterAllAreas();
            //RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);


            log4net.Config.XmlConfigurator.Configure();

        }

        void Application_End(object sender, EventArgs e)
        {
            //try
            //{
            //    Enter.Services.DataCache.ICache icache = Unity.GetInstance<Enter.Services.DataCache.ICache>();
            //    icache.ReleaseConnection();
            //}
            //catch { }
            //  在应用程序关闭时运行的代码
        }

        private void OnStart()
        {
            setupContiner();

			UnityBaseContiner();

            registerModelBinders(GetBaseModelBinders());

            registerActionFilters(GetBaseActionFilters());

            registerViewEngines();

            registerControllerFactory();

        }

        #region OnStart

        private void setupContiner()
        {
            Application.Add("container", ContainerFactory.Current);
        }

        protected IUnityContainer getContainer()
        {
            return (IUnityContainer)Application["container"];
        }

		protected virtual void UnityBaseContiner()
		{
			IUnityContainer container = getContainer();
			UnityBase site = container.Resolve<IUnityBaseService>().GetBase();
			if (site != null)
			{
				container.RegisterInstance(site);
			}
		}


        /// <summary>
        /// 从Html提交数据 自动转化成类
        /// </summary>
        protected virtual void registerModelBinders(ModelBinderDictionary modelBinder)
        {

        }

        /// <summary>
        /// 标签式编程 统一书写 请注意 最后增加的最先执行 
        /// </summary>
        /// <param name="registry"></param>
        protected virtual void registerActionFilters(IActionFilterRegistry registry)
        {
            IUnityContainer container = getContainer();
            container.RegisterInstance(registry);
        }

        private void registerViewEngines()
        {
           ViewEngines.Engines.Clear();
           ViewEngines.Engines.Add(getContainer().Resolve<IViewEngine>());
        }

        private void registerControllerFactory()
        {
            ControllerBuilder.Current.SetControllerFactory(getContainer().Resolve<MyChyControllerFactory>());
        }

        #endregion

        #region 重载定义使用


        private ModelBinderDictionary GetBaseModelBinders()
        {
            ModelBinderDictionary binders = System.Web.Mvc.ModelBinders.Binders;
            return binders;
        }

        /// <summary>
        /// 请注意 最后增加的最先执行 
        /// </summary>
        /// <returns></returns>
        private IActionFilterRegistry GetBaseActionFilters()
        {
            IUnityContainer container = getContainer();
            IActionFilterRegistry registry = container.Resolve<ActionFilterRegistry>();
            registry.Clear();
            return registry;
        }



        #endregion

        #region 默认路由

        /// <summary>
        /// 默认路由
        /// </summary>
        /// <param name="routes"></param>
        private void General(RouteCollection routes)
        {
            routes.MapRoute(
            "GeneralTrace",
            "general/trace",
            new { controller = "General", action = "Trace" },
            new[] { "Enter.Mvc.Base.Controllers" }
            );

            routes.MapRoute(
            "GeneralLogininfo",
            "general/logininfo",
            new { controller = "General", action = "Logininfo" },
            new[] { "Enter.Mvc.Base.Controllers" }
            );

            routes.MapRoute(
                "Shared_PageTop",
                "general/pagetop/",
                new { controller = "General", action = "PageTop" },
            new[] { "Enter.Mvc.Base.Controllers" }
                );
            routes.MapRoute(
                "Shared_PageHead",
                "general/pagehead/",
                new { controller = "General", action = "PageHead" },
            new[] { "Enter.Mvc.Base.Controllers" }
                );
            routes.MapRoute(
                "Shared_PageFoot",
                "general/pagefoot/",
                new { controller = "General", action = "PageFoot" },
            new[] { "Enter.Mvc.Base.Controllers" }
                );
        }

        #endregion

    }
}
